clear all

cd "$US_Ineq_Repl"
import excel "$US_Ineq_Repl/Data/cpi_mwage.xlsx", sheet("Sheet1") firstrow clear

save "$US_Ineq_Repl/Processed/CPI_mwage.dta", replace

local Data "$US_Ineq_Repl/Processed/CEPR"
cd `Data'

local year_b=2020

foreach year in 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 /*
			*/	1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 /*
			*/  1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 /*
			*/  2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 /*
			*/  2019 2020 {
	*minimum wage and CPI
	use "$US_Ineq_Repl/Processed/CPI_mwage.dta", replace

	local row=`year'-1977
	local cpi=cpi in `row'
	local mwage=minwage in `row'
	
	local row=`year_b'-1977
	local cpi_b=cpi in `row'
	local row=1979-1977
	local cpi79=cpi in `row'
	
	local lower=1*(`cpi'/`cpi79')
	local upper=100*(`cpi'/`cpi79')
	di `lower'
	di `upper'
	
	use "cepr_org_`year'.dta", replace
	*transform to `year_b' usd!
	gen rwage3=wage3*(`cpi_b'/`cpi')
	local realminw=`mwage'*(`cpi_b'/`cpi')
	local lrmwage=ln(`realminw')
	keep if 16<=age & age<=65
	keep if `lower'<=rwage3 & rwage3 <=`upper'
	keep if rwage3!=.
	
	gen lrwage3=ln(rwage3)
	sum lrwage3, detail
	gen xstep=(r(max)-r(min))/200
	*kwage will be the wage at which the density is estimated;
	gen kwage=r(min)+(_n-1)*xstep if _n<=200

	* hours weighted
	gen hweight=orgwgt*uhourse
	
		kdensity lrwage3 [aweight=orgwgt] if female==0 , at(kwage) gauss width(0.065) /*
	*/generate(w`year'm fd`year'm) nograph 

	kdensity lrwage3 [aweight=orgwgt] if female==1 , at(kwage) gauss width(0.065) /*
	*/generate(w`year'f fd`year'f) nograph 
	
	label var fd`year'm "Male `year'"
	label var fd`year'f "Female `year'"
	label var kwage "Log(Wage)"
	
	local file = "CHmen_`year'_rlwage.png"
	capture confirm file "$US_Ineq_Repl/Results/Figures/`file'"
	
	if _rc { 
		graph twoway (connected fd`year'm kwage if kwage>=1.5 & kwage<=4.7, msymbol(i) /*
		*/ lwidth(thick) xline(`lrmwage',lwidth(medthin)) plotregion(style(none) color(white) fcolor(white)) graphregion(style(none) color(white) fcolor(white))/*
		*/ title("`year'",size(huge)) subtitle("") xtitle("") ytitle("") /*
		*/ xlabel(1.5 "ln(4)" 2.2 "ln(9)" 2.9 "ln(19)" 3.7 "ln(39)" 4.4 "ln(80)",labsize(huge)) /*
		*/ ylabel(0 0.25 0.5 0.75 1, nogrid angle(horizontal) labsize(huge) )  yscale(range(0 1)))

		graph export "$US_Ineq_Repl/Results/Figures/`file'", as(png) replace
	}
	
	local file = "CHwomen_`year'_rlwage.png"
	capture confirm file "$US_Ineq_Repl/Results/Figures/`file'"
	
	if _rc { 
		graph twoway (connected fd`year'f kwage if kwage>=1.4 & kwage<=4.7, msymbol(i) /*
		*/ lwidth(thick) xline(`lrmwage',lwidth(medthin)) plotregion(fcolor(white)) graphregion(fcolor(white))/*
		*/ title("`year'",size(huge)) subtitle("") xtitle("") ytitle("") /*
		*/ xlabel(1.5 "ln(4)" 2.2 "ln(9)" 2.9 "ln(19)" 3.7 "ln(39)" 4.4 "ln(80)",labsize(huge)) /*
		*/ ylabel(0 0.25 0.5 0.75 1 1.25, nogrid angle(horizontal) labsize(huge) )  yscale(range(0 1.25)))
			
		graph export "$US_Ineq_Repl/Results/Figures/`file'", as(png) replace
	}
	
}
